package com.activiti.gateway;

import com.activiti.bean.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.HashMap;
import java.util.Map;

/**
 * 包含网关
 * @author ldanniel
 */
public class InclusiveGateway {
    public static void main(String[] args) {
        InclusiveGateway inclusiveGateway = new InclusiveGateway();
        inclusiveGateway.commitProcess();
    }
    public void commitProcess(){
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //通过任务服务接口获取，参数：流程id,负责人
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("InclusiveGateway")
                .taskAssignee("王经理")
                .singleResult();
        //根据任务id完成任务
        if(null != task){
            taskService.complete(task.getId());
            System.out.println("流程id = " + task.getProcessInstanceId());
            System.out.println("任务id = " + task.getId());
            System.out.println("负责人 = " + task.getAssignee());
            System.out.println("任务名称 = " + task.getName());
            System.out.println("执行任务完成");
        }else {
            System.out.println("未找到任务");
        }
    }
    public void startProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> evectionMap = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(4d);
        evectionMap.put("evection",evection);
        evectionMap.put("assignee0","张三");
        evectionMap.put("assignee1","王经理");
        evectionMap.put("assignee2","张总监");
        evectionMap.put("assignee3","周人事经理");
        evectionMap.put("assignee4","李总经理");

        //启动流程
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("InclusiveGateway", evectionMap);
        System.out.println("instance name == " + instance.getName());
        System.out.println("instance id == " + instance.getId());
    }

    /**
     * 读取流程图
     */
    public void deployment() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                //禁用架构验证
                .disableSchemaValidation()
                .name("包含网关-InclusiveGateway")
                .addClasspathResource("bpmn/InclusiveGateway.bpmn")
                .deploy();
        System.out.println("部署流程的id" + deploy.getId());
        //部署流程的名称为空，是因为压缩包中可能有多个流程图，所以不能统一命名
        System.out.println("部署流程的名称" + deploy.getName());
    }
}
